package rpc

import (
	"gitee.com/kinwyb/appTools/common"
	"gitee.com/kinwyb/appTools/examples/src/rpc/serverRpc"
	"github.com/sirupsen/logrus"
	"github.com/smallnest/rpcx/client"
)

type RpcxClient struct {
	selector client.Selector
}

func (r *RpcxClient) InitClients(discovery client.ServiceDiscovery, selector client.Selector) {
	r.selector = selector
	sel := client.RoundRobin
	if r.selector != nil {
		sel = client.SelectByUser
		serverRpc.ClientInitDoneCallback = r.rpcClientInitDoneCallback
	}
	serverRpc.RpcxClients(discovery, client.Failover, sel, client.DefaultOption)
}

func (r *RpcxClient) rpcClientInitDoneCallback(c client.XClient) {
	if r.selector != nil {
		newSelector := common.InterfaceClone(r.selector)
		c.SetSelector(newSelector.(client.Selector))
	}
}

func (r *RpcxClient) Close() {
	serverRpc.RpcxClose()
}

func (r *RpcxClient) SetLog(entry *logrus.Entry) {
	serverRpc.SetLogger(entry)
}
